package com.example.sp1902;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.sp1902.fpage.FirstPageFragment;
import com.example.sp1902.goods.GoodsFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,View.OnClickListener {
    RadioGroup mainRg;
    List<Fragment>fList;
    FragmentManager fmanager;
    Button syBtn,typeBtn,meBtn,logBtn,regBtn,exitBtn;
    TextView logTv;
    DrawerLayout drawerLayout;
    RadioButton syRb,typeRb,meRb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainRg = findViewById(R.id.main_rg);
        mainRg.setOnCheckedChangeListener(this);//设置监听
        initFrag();
        initView();
    }
//将左边的抽屉里控件初始化
    private void initView() {
        syRb = findViewById(R.id.main_rb_fpage);
        typeRb = findViewById(R.id.main_rb_prod);
        meRb = findViewById(R.id.main_rb_me);

        syBtn =findViewById(R.id.main_left_btn_fp);
        meBtn =findViewById(R.id.main_left_btn_me);
        typeBtn =findViewById(R.id.main_left_btn_type);
        logBtn =findViewById(R.id.main_left_btn_login);
        regBtn =findViewById(R.id.main_left_btn_reg);
        exitBtn =findViewById(R.id.main_left_btn_exit);
        logTv =findViewById(R.id.main_left_tv1);
        drawerLayout = findViewById(R.id.main_drawer);
        syBtn.setOnClickListener(this);
        meBtn.setOnClickListener(this);
        typeBtn.setOnClickListener(this);
        logBtn.setOnClickListener(this);
        regBtn.setOnClickListener(this);
        exitBtn.setOnClickListener(this);
    }

    private void initFrag() {
        fList = new ArrayList<>();
        fList.add(new FirstPageFragment());
        fList.add(new GoodsFragment());
        fList.add(new MeFragment());
        fmanager = getSupportFragmentManager();
        FragmentTransaction ft = fmanager.beginTransaction();
        for (int i = 0;i <fList.size();i++){
            ft.add(R.id.main_layout,fList.get(i));
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(0));
        ft.commit();
    }
    //切换Fragment的方法
    private void changeFrag(int pos){
        FragmentTransaction ft = fmanager.beginTransaction();
        for (int i = 0;i <fList.size();i++){
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(pos));
        ft.commit();

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.main_rb_fpage:
                changeFrag(0);
                break;
            case R.id.main_rb_prod:
                changeFrag(1);
                break;
            case R.id.main_rb_me:
                changeFrag(2);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_left_btn_fp:
                syRb.setChecked(true);
                break;
            case R.id.main_left_btn_type:
                typeRb.setChecked(true);
                break;
            case R.id.main_left_btn_me:
                meRb.setChecked(true);
                break;
            case R.id.main_left_btn_login:
                break;
            case R.id.main_left_btn_reg:
                break;
            case R.id.main_left_btn_exit:
                break;
        }
        drawerLayout.closeDrawers();//关闭抽屉
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
